package stancel.peter.smartsilencer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.preference.PreferenceManager;

public class SmartSilencer extends Application{

	@Override
	public void onCreate() {
		super.onCreate();
		
		/**
		 * Nastavy predvolene hodnoty z preferences.xml. Spravi to len pri prvom
		 * spusteni aplikacie.
		 */
		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
	}

	/**
	 * Nacita list udalosti zo suboru.
	 * @return vrati nacitany list udalosti.
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<Event> loadFromFile() {
		List<Event> list = new ArrayList<Event>();
		try {
			FileInputStream fis = openFileInput("ListOfEvents.dat");
			ObjectInputStream ois = new ObjectInputStream(fis);
			list = (List<Event>) ois.readObject();

			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			// The file doesn't exist.
			// It may occur when we launch this application for the first time.
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * Ulozi list udalosti do suboru.
	 */
	public synchronized void saveListToFile(List<Event> list) {
		try {
			FileOutputStream fos = openFileOutput("ListOfEvents.dat", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(list);

			fos.flush();
			oos.flush();
			fos.close();
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public synchronized List<TimeAlarm> loadDatesFromFile() {
		List<TimeAlarm> list = new ArrayList<TimeAlarm>();
		try {
			FileInputStream fis = openFileInput("ListOfDates.dat");
			ObjectInputStream ois = new ObjectInputStream(fis);
			list = (List<TimeAlarm>) ois.readObject();

			fis.close();
			ois.close();
		} catch (FileNotFoundException e) {
			// The file doesn't exist.
			// It may occur when we launch this application for the first time.
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public synchronized void saveDatesListToFile(List<TimeAlarm> timeAlarmList) {
		Collections.sort(timeAlarmList);
		try {
			FileOutputStream fos = openFileOutput("ListOfDates.dat", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(timeAlarmList);

			fos.flush();
			oos.flush();
			fos.close();
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
